<template>
    <div>
        <PageHeader />
        <PageNav />
        <div class="list">
            <div class="box">
                <div class="path">首页 / 填写订单信息</div>
                <div class="detail">

                    <!--收件人信息-->
                    <div style="margin-bottom:30px;">
                        <div style="text-align:left;margin-bottom: 10px;">收件人信息</div>
                        <div style="border:1px solid #dcdcdc; padding:20px;margin-bottom: 40px;">
                            <el-row>
                                <el-col :span="8" v-for="(o, index) in addressData" :key="index">
                                    <el-card :body-style="{ padding: '0px' }"
                                        :class="[{ 'activ': changeSelectCardBackground == index }, { 'mycard': true }]">
                                        <div style="padding: 14px; text-align: left; height: 90px;"
                                            @click="eventCardClick(o, index)">
                                            <div>收件人：{{ o.name }}</div>
                                            <div>收件电话：{{ o.phone }}</div>
                                            <div>收件地址：{{ o.address }}</div>
                                        </div>
                                        <div class="option">
                                            <div style="text-align:right;">
                                                <i class="el-icon-edit opt" @click="eventEditAddress"></i> |
                                                <i class="el-icon-delete opt" @click="eventDeleteAddress(o.id)"></i>
                                            </div>
                                        </div>
                                    </el-card>
                                </el-col>

                                <!--添加按钮-->
                                <el-col :span="8">
                                    <el-card :body-style="{ padding: '0px' }" style="height:118px;">
                                        <div style="padding: 14px; cursor: pointer; " @click="dialogAddAddress">
                                            <div style="font-size:60px;">+</div>
                                        </div>
                                    </el-card>
                                </el-col>
                            </el-row>
                        </div>
                    </div>

                    <!--配送信息-->
                    <div style="margin-bottom:30px;">
                        <div style="text-align:left;margin-bottom: 10px;">配送信息</div>
                        <div style="border:1px solid #dcdcdc; padding:20px; ">
                            <el-row>
                                <el-col :span="6">
                                    <div class="send-item send-activ">不限配送时间：<span>周一到周日</span></div>
                                </el-col>
                                <el-col :span="6">
                                    <div class="send-item">工作日送化：<span>周一到周五</span></div>
                                </el-col>
                                <el-col :span="6">
                                    <div class="send-item">双休、假日送货：<span>周六、周日</span></div>
                                </el-col>
                                <el-col :span="6"></el-col>
                            </el-row>
                        </div>
                    </div>

                    <!--支付信息-->
                    <div style="margin-bottom:30px;">
                        <div style="text-align:left;margin-bottom: 10px;">支付信息</div>
                        <div style="border:1px solid #dcdcdc; padding:20px; ">
                            <el-row>
                                <el-col :span="6">
                                    <div class="send-item send-activ">在线支付</div>
                                </el-col>
                                <el-col :span="6">
                                    <div class="send-item">货到付款</div>
                                </el-col>
                                <el-col :span="12">
                                    <div style="font-size:10px; text-align: left; line-height: 45px;">
                                        货到付款需付5元手续费，仅支持2000元以下订单</div>
                                </el-col>
                            </el-row>
                        </div>
                    </div>

                    <!--发票信息-->
                    <div style="margin-bottom:30px;">
                        <div style="text-align:left;margin-bottom: 10px;">发票信息</div>
                        <div style="border:1px solid #dcdcdc; padding:20px; ">
                            <el-row>
                                <el-col :span="6">
                                    <div class="send-item">
                                        <el-checkbox>我需要开发票</el-checkbox>
                                    </div>
                                </el-col>
                                <el-col :span="6">

                                </el-col>
                                <el-col :span="12">

                                </el-col>
                            </el-row>
                        </div>
                    </div>

                    <!--商品信息-->
                    <div style="margin-bottom:30px;">
                        <div style="text-align:left;margin-bottom: 10px;">商品信息</div>
                        <div style="border:1px solid #dcdcdc; padding:20px; ">
                            <el-table :data="carData" style="width: 100%">
                                <el-table-column label="商品图片" width="180">
                                    <template slot-scope="scope">
                                        <img :src="'http://localhost:5286/' + scope.row.coverImg" class="image"
                                            style="width:50px;" />
                                    </template>
                                </el-table-column>
                                <el-table-column prop="goodsName" label="商品名称" width="180">
                                </el-table-column>
                                <el-table-column prop="goodsPrice" label="商品价格">
                                </el-table-column>
                                <el-table-column label="商品数量" prop="goodsNumber">
                                </el-table-column>
                                <el-table-column label="小计">
                                    <template slot-scope="scope">
                                        {{ scope.row.goodsPrice * scope.row.goodsNumber }}
                                    </template>
                                </el-table-column>
                            </el-table>
                        </div>
                    </div>

                    <!--金额信息-->
                    <div style="margin-bottom:30px;">
                        <div style="text-align:left;margin-bottom: 10px;">金额信息</div>
                        <div style="border-top:1px solid #dcdcdc; padding:20px; text-align: right; ">
                            <el-row>
                                <el-col :span="21">商品件数：</el-col>
                                <el-col :span="3">5</el-col>
                            </el-row>
                            <el-row>
                                <el-col :span="21">商品总价：</el-col>
                                <el-col :span="3"> ￥0.00</el-col>
                            </el-row>
                            <el-row>
                                <el-col :span="21">活动优惠：</el-col>
                                <el-col :span="3">- ￥0.00</el-col>
                            </el-row>
                            <el-row>
                                <el-col :span="21">优惠券抵扣：</el-col>
                                <el-col :span="3">- ￥0.00</el-col>
                            </el-row>
                            <el-row>
                                <el-col :span="21">运费：</el-col>
                                <el-col :span="3">- ￥0.00</el-col>
                            </el-row>
                            <el-row>
                                <el-col :span="21">应付总金额：</el-col>
                                <el-col :span="3">￥125740.00</el-col>
                            </el-row>
                        </div>
                    </div>

                    <div style="margin-bottom:30px; text-align: right;">
                        <el-button type="danger" style="border-radius:0;width:200px; height:50px;font-size:20px"
                            @click="eventAddOrder">立即下单</el-button>
                    </div>
                </div>
            </div>
        </div>
        <PageCustom />
        <PageFooter />



        <!--弹出对话框-->
        <el-dialog title="收货地址" :visible.sync="dialogFormVisible" style="text-align:left;">
            <el-form :model="addressForm">
                <el-form-item label="收件人姓名" :label-width="formLabelWidth">
                    <el-input v-model="addressForm.name" size="mini" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="收件人电话" :label-width="formLabelWidth">
                    <el-input v-model="addressForm.phone" size="mini" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="所在城市" :label-width="formLabelWidth">
                    <el-select v-model="addressForm.city1" size="mini" placeholder="请选择省份" style="width:145px;"
                        @change="eventCity1Change">
                        <el-option v-for="(item, index) in city1Data" :key="index" :label="item.name"
                            :value="item.id"></el-option>
                    </el-select>
                    <el-select v-model="addressForm.city2" size="mini" placeholder="请选择城市" style="width:145px;"
                        @change="eventCity2Change">
                        <!-- <el-option label="区域一" value="shanghai"></el-option>
                        <el-option label="区域二" value="beijing"></el-option> -->
                        <el-option v-for="(item, index) in city2Data" :key="index" :label="item.name"
                            :value="item.id"></el-option>
                    </el-select>
                    <el-select v-model="addressForm.city3" size="mini" placeholder="请选择区域" style="width:145px;">
                        <!-- <el-option label="区域一" value="shanghai"></el-option>
                        <el-option label="区域二" value="beijing"></el-option> -->
                        <el-option v-for="(item, index) in city3Data" :key="index" :label="item.name"
                            :value="item.id"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="详细地址" :label-width="formLabelWidth">
                    <el-input v-model="addressForm.address" size="mini" type="textarea" :rows="2"
                        autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="" :label-width="formLabelWidth">
                    <el-checkbox v-model="addressForm.isDefault">设置默认</el-checkbox>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="eventAddAddress">确 定</el-button>
            </div>
        </el-dialog>

    </div>
</template>

<script>

//引入组件页面
import PageNav from '@/components/PageNav.vue'
import PageHeader from '@/components/PageHeader.vue'
import PageCustom from '@/components/PageCustom.vue'
import PageFooter from '@/components/PageFooter.vue'
export default {
    components: {
        //声明刚才我们引入的页面，是一个组件
        PageNav, PageHeader, PageCustom, PageFooter
    },
    data() {
        return {

            city1Data: [],
            city2Data: [],
            city3Data: [],
            dialogFormVisible: false,
            addressForm: {
                name: '',
                phone: '',
                city1: '',
                city2: '',
                city3: '',
                address: '',
                isDefault: false
            },
            formLabelWidth: '120px',

            selectedAddressInfo: 0,  //选中的收件人信息
            changeSelectCardBackground: 0,
            carData: [],
            addressData: [
                { id: 1, name: 'abce', phone: '18012345678', address: '上海浦东' },
                { id: 2, name: 'jdks', phone: '18065485678', address: '上海浦东新构柯翰辰工村的发生山大佛俄方的是199号149室' },
                // {id:3,name:'tmax',phone:'18439483475',address:'上海浦东'},
                // {id:4,name:'cga',phone:'18012985678',address:'上海浦东'},
            ]
        }
    },
    methods: {
        //提交订单按钮事件
        eventAddOrder() {



            //会员
            let memberId = localStorage.getItem('liangcaigen')

            let _newData = []
            //商品信息
            let total = 0  //总金额
            this.carData.forEach(x => {
                total += x.goodsNumber * x.goodsPrice

                //前台和后台定义的数据属性字段不一样，所以重新构建后台需要的数据
                _newData.push(
                    {
                        "goodsId": x.goodsId,
                        "goodsName": x.goodsName,
                        "goodsPrice": x.goodsPrice,
                        "goodsCount": x.goodsNumber
                    }
                )
            })

            //运费（总金额超过200元，免运费
            let freight = 10
            if (total > 200)
                freight = 0

            //应付金额（商品总金额+运费）
            let payTotal = total + freight

            let url = 'http://localhost:5286/order/add'
            let data = {
                "order": {
                    "memberId": memberId,
                    "orderTotal": total,
                    "rate": 0, //税率暂时没用到，不用管
                    "payTotal": payTotal,
                    "freight": freight,
                    "recivedName": this.selectedAddressInfo.name,
                    "recivedPhone": this.selectedAddressInfo.phone,
                    "recivedAddress": this.selectedAddressInfo.address
                },
                "orderDetail": _newData
            }
            //console.log(url + '' + data)
            this.$http.post(url, data).then(res => {
                console.log(res)
                if (res.data.code == 200) {
                    //跳转到下单成功页面，去生成支付页面
                    //this.$message(res.data.message + '---订单号：' + res.data.data)
                    this.$router.push({path:'/car/pay',query:{orderNo: res.data.data }})
                }
            })
        },

        //弹出添加地址
        dialogAddAddress() {
            this.dialogFormVisible = true

            //在弹出添加地址的对话框的时候，才需要加载下拉框
            this.onloadCity1()
        },

        /**
         * 下拉相关
         * 
         */
        onloadCity1() {
            let url = 'http://localhost:5286/city/list?id=1'
            this.$http.get(url).then(res => {
                this.city1Data = res.data.data
            })
        },
        onloadCity2(id) {
            let url = 'http://localhost:5286/city/list?id=' + id
            this.$http.get(url).then(res => {
                this.city2Data = res.data.data
            })
        },
        onloadCity3(id) {
            let url = 'http://localhost:5286/city/list?id=' + id
            this.$http.get(url).then(res => {
                this.city3Data = res.data.data
            })
        },

        //下拉框的改变事件
        eventCity1Change(val) {
            this.onloadCity2(val)
        },
        eventCity2Change(val) {
            this.onloadCity3(val)
        },
        /**
         * 下拉相关结束
         */


        //添加地址确认按钮
        eventAddAddress() {
            let url = 'http://localhost:5286/member/address/add'
            let data = {
                "memberId": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
                "name": this.addressForm.name,
                "phone": this.addressForm.phone,
                "address": this.addressForm.address,
                "provite": this.addressForm.city1.toString(),
                "city": this.addressForm.city2.toString(),
                "area": this.addressForm.city3.toString(),
                "isDefault": this.addressForm.isDefault == true ? "1" : "0"
            }

            //console.log(this.addressForm)
            this.$http.post(url, data).then(res => {
                if (res.data.code == 200) {
                    this.dialogFormVisible = false

                    //重新加载地址列表
                    this.onloadAddress()
                }
            })
        },

        //加载会员的地址列表
        onloadAddress() {
            let url = 'http://localhost:5286/member/address/query?memberId=3fa85f64-5717-4562-b3fc-2c963f66afa6'
            this.$http.get(url).then(res => {
                console.log(res)
                this.addressData = res.data.data
            })
        },

        //地址编辑按钮
        eventEditAddress() {
            //this.$message('这是编辑按钮单击事件')
            this.dialogFormVisible = true
            this.onloadCity1();
            this.city1 = '130000' //表示选中的

            this.onloadCity2(this.city1)
            this.addressForm.city2 = '130300'

            this.onloadCity3(this.city2)
            this.addressForm.city3 = 130303
        },

        //删除地址
        eventDeleteAddress(id) {
            this.$confirm('此操作将永久删除, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                let url = 'http://localhost:5286/member/address/delete?id=' + id
                this.$http.delete(url).then(res => {
                    if (res.data.code == 200) {
                        this.onloadAddress()
                    }
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });

        },

        //卡片单击事件
        eventCardClick(row, index) {
            //这个事件里，可以获取用户选中的收件人地址信息
            this.selectedAddressInfo = row
            this.changeSelectCardBackground = index



        }
    },
    mounted() {

        //在购物车列表中，选中的商品，是以字符串的形式传过来的，所以我们收到的，也是字符串（json字符串）
        let d = localStorage.getItem('selectCarData')
        //console.log(d)
        //如果要用到这些字符串数据，则需要转换为json对象
        this.carData = JSON.parse(d)
        //console.log(this.carData)

        this.onloadAddress()
    }
}
</script>



<style scoped>
.activ {
    background-color: rgb(127, 221, 255);
}

.mycard {
    position: relative;
}

.option {
    display: none;
    position: absolute;
    bottom: 0;
    right: 0;
}

.opt-select {
    position: absolute;
    top: 0;
    right: 0;
}

.mycard:hover .option {
    display: block;
}

.opt {
    cursor: pointer;
}

.send-item {

    min-width: 220px;
    text-align: center;
    background: #eee;
    height: 45px;
    line-height: 45px;
    padding: 0 10px;
    margin-right: 30px;
    cursor: pointer;
}

.send-item span {
    color: #5d5b5b;
}

.send-activ {
    border: 1px solid red;
    color: red;
}

.send-activ span {
    color: red;
}
</style>